package actionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import util.DomainUtils;

public class SearchContactValidationForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String keywords = null;

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = (keywords == null) ? "" : DomainUtils.trim(keywords);
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.keywords = null;
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession(false);


		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			errors.add(
					DomainUtils.ALERT_ERROR_STRING,
					new ActionMessage(
							"session.error"));

			DomainUtils.keepParametersContent(request);

			return errors;
		}

		
//Jean-Luc: Permit to be empty for match all things
//		if (keywords == null || keywords.isEmpty()) {
//			errors.add(
//					"keywords",
//					new ActionMessage(
//							"search.notempty.error"));
//
//			DomainUtils.keepParametersContent(request);
//
//			return errors;
//		}

		return errors;
	}

}
